package com.zhentao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.Result;
import com.zhentao.domain.SysBattery;
import com.zhentao.domain.SysSite;
import com.zhentao.domain.SysSiteRecord;
import com.zhentao.domain.SysUser;
import com.zhentao.domain.dto.PageDto;
import com.zhentao.domain.vo.Map;
import com.zhentao.service.SysBatteryService;
import com.zhentao.service.SysSiteRecordService;
import com.zhentao.mapper.SysSiteRecordMapper;
import com.zhentao.service.SysSiteService;
import com.zhentao.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86186
* @description 针对表【sys_site_record】的数据库操作Service实现
* @createDate 2024-09-08 15:47:21
*/
@Service
public class SysSiteRecordServiceImpl extends ServiceImpl<SysSiteRecordMapper, SysSiteRecord>
    implements SysSiteRecordService{
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysBatteryService sysBatteryService;;
    @Autowired
    private SysSiteService sysSiteService;

    @Override
    public Result getData(PageDto pageDto) {
        boolean flag=false;boolean flag1 = false;boolean flag2 = false;
        List<Integer> uid=new ArrayList<>();List<Integer> batteryId =new ArrayList<>();List<Integer> siteId =new ArrayList<>();
        Page<SysSiteRecord> page = new Page<>(pageDto.getPage(), pageDto.getSize());
        if(pageDto.getMap().size()>0){
            if (StringUtils.checkValNotNull(pageDto.getMap().get("uid"))){
                List<SysUser> u = sysUserService.list(Wrappers.<SysUser>lambdaQuery().like(SysUser::getNickname, pageDto.getMap().get("uid")));
                if (u.size()>0){
                    List<Integer> collect = u.stream().map(SysUser::getUid).collect(Collectors.toList());
                    pageDto.getMap().replace("uid",collect);
                    flag=true;
                }
            }
            if (StringUtils.checkValNotNull(pageDto.getMap().get("siteId"))){
                List<SysSite> s = sysSiteService.list(Wrappers.<SysSite>lambdaQuery().like(SysSite::getAddress, pageDto.getMap().get("siteId")));
                if (s.size()>0){
                    List<Integer> collect = s.stream().map(SysSite::getId).collect(Collectors.toList());
                    pageDto.getMap().replace("siteId",collect);
                    flag1=true;
                }
            }
            if (StringUtils.checkValNotNull(pageDto.getMap().get("batteryId"))){
                List<SysBattery> b = sysBatteryService.list(Wrappers.<SysBattery>lambdaQuery().like(SysBattery::getBatteryName, pageDto.getMap().get("batteryId")));
                if (b.size()>0){
                    List<Integer> collect = b.stream().map(SysBattery::getId).collect(Collectors.toList());
                    pageDto.getMap().replace("batteryId",collect);
                    flag2=true;
                }
            }
            if (flag) uid= (List<Integer>) pageDto.getMap().get("uid");
            if (flag2) batteryId = (List<Integer>) pageDto.getMap().get("batteryId");
            if (flag1) siteId = (List<Integer>) pageDto.getMap().get("siteId");
            System.out.println(uid+"---"+siteId+"---"+batteryId);
            this.page(
                    page,
                    Wrappers.<SysSiteRecord>lambdaQuery()
                            .in(uid.size()>0,SysSiteRecord::getUid,uid)
                            .in(siteId.size()>0,SysSiteRecord::getSiteId,siteId)
                            .in(batteryId.size()>0,SysSiteRecord::getBatteryId,batteryId)
                            .eq(StringUtils.checkValNotNull(pageDto.getMap().get("status")),SysSiteRecord::getStatus,pageDto.getMap().get("status"))
                            .orderByDesc(SysSiteRecord::getCreateTime));
        }else{
            this.page(page,Wrappers.<SysSiteRecord>lambdaQuery().orderByDesc(SysSiteRecord::getCreateTime));
        }
        for (SysSiteRecord record : page.getRecords()) {
            if (StringUtils.checkValNotNull(record.getUid())){
                SysUser sysUser = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUid, record.getUid()));
                record.setSysUser(sysUser);
            }
            if (StringUtils.checkValNotNull(record.getBatteryId())){
                SysBattery sysBattery = sysBatteryService.getOne(Wrappers.<SysBattery>lambdaQuery().eq(SysBattery::getId, record.getBatteryId()));
                 record.setSysBattery(sysBattery);
            }
            if (StringUtils.checkValNotNull(record.getSiteId())){
                SysSite sysSite = sysSiteService.getOne(Wrappers.<SysSite>lambdaQuery().eq(SysSite::getId, record.getSiteId()));
                record.setSysSite(sysSite);
            }
        }
        return Result.build(page, 200, "查询成功");
    }
}




